package com.three.controller; /**
 * @author wzm
 * @create 2023-11-07 16:53
 * project_name:web2310
 * @Description
 */

import com.three.pojo.User;
import com.three.service.UserService;
import com.three.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

//当前路径需要匹配关于User相关的所有功能  如：/user/login登录    /user/add 新增
@WebServlet("/user/*")
public class UserServlet extends BaseServlet {

    private UserService userService = new UserServiceImpl();

    //新增
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("add");
    }


    //删除

    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    //修改

    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    //登陆
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //实现登录功能

    /*    //0.设置编码方式
        request.setCharacterEncoding("UTF-8");//请求
        response.setContentType("text/html;charset=UTF-8");//响应*/

        //1.接收前端参数用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //2.调用业务逻辑层servcie实现登录的操作
        User login = userService.login(new User(null, username, password));


        //3.根据结果进行判断

        PrintWriter writer = response.getWriter();
        if (login != null) {
            //登录成功
            //4.响应结果到浏览器
            writer.write("登录成功");
        } else {
            //登录失败
            writer.write("登录失败");
        }
    }

    //注册
    protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
        //1.设置请求和响应的编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
*/

        //2.获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //3.将请求参数封装为对象
        User user = new User(null, username, password);

        //4.访问Service进行操作
        //验证用户名是否存在
        boolean exists = userService.existsName(username);
        PrintWriter writer = response.getWriter();

        //5.判断用户对象是否为null
        if (exists) { //返回true不存在
            //用户名不存在
            //执行新增 Servcie
            boolean register = userService.register(user);
            if (register) {
                writer.write("注册成功");
            } else {
                writer.write("注册失败");
            }

        } else {
            //用户名已经存在 各处提示信息
            writer.write("用户名已经存在，请更换！！");
        }

    }

    //模糊查询
    protected void likename(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }


}
